package enron;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class BuyerAgentUI extends JFrame{
	private static final long serialVersionUID = 1465833460254799927L;
	
	private BuyerAgent myAgent;
	private JPanel contentPane;
	private JLabel lblWattageValue;
	private JLabel lblEnergyValue;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BuyerAgentUI window = new BuyerAgentUI(null);
					window.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @param buyer 
	 */
	public BuyerAgentUI(BuyerAgent buyer) {
		myAgent = buyer;
		initGUI();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initGUI() {
		setTitle(myAgent.getLocalName());
		this.setBounds(100, 100, 300, 200);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowListener() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				((BuyerAgentUI)arg0.getComponent()).myAgent.doDelete();
				//.doDelete();
			}

			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setName("contentPane");
		contentPane.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.PREF_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("pref:grow"),}));
		{
			JLabel lblWattage = new JLabel("Wattage:");
			contentPane.add(lblWattage, "1, 1, right, default");
			lblWattage.setName("lblWattage");
		}
		{
			lblWattageValue = new JLabel(myAgent.getPower().toString() + "W");
			contentPane.add(lblWattageValue, "3, 1, left, default");
			lblWattageValue.setName("lblWattageValue");
		}
		{
			JLabel lblEnergyUsage = new JLabel("Energy usage:");
			contentPane.add(lblEnergyUsage, "1, 3, right, default");
			lblEnergyUsage.setName("lblEnergyUsage");
		}
		{
			lblEnergyValue = new JLabel(BuyerAgent.energyFormat.format(myAgent.getEnergyUsed()*1000.0) + "Wh");
			contentPane.add(lblEnergyValue, "3, 3, left, default");
			lblEnergyValue.setName("lblEnergyValue");
		}
		{
			TitledBorder applianceBorder = new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Appliances", TitledBorder.LEADING, TitledBorder.TOP, null, null);
			JPanel appliancePanelOuter = new JPanel();
			appliancePanelOuter.setName("appliancePanelOuter");
			contentPane.add(appliancePanelOuter, "1, 5, 3, 1, fill, fill");
			//appliancePanelOuter.setBorder(applianceBorder);
			appliancePanelOuter.setLayout(new BorderLayout(0, 0));
			{
				JScrollPane applianceScrollPane = new JScrollPane();
				applianceScrollPane.setBorder(applianceBorder);
				//applianceScrollPane.setViewportBorder();
				applianceScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
				applianceScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
				applianceScrollPane.setName("applianceScrollPane");
				appliancePanelOuter.add(applianceScrollPane);
				{
					JTable applianceTable = new JTable(); 
					applianceTable.setTableHeader(null);
					applianceScrollPane.setColumnHeaderView(null);
					applianceTable.setShowVerticalLines(false);
					applianceTable.setShowHorizontalLines(false);
					applianceTable.setShowGrid(false);
					applianceTable.setRowSelectionAllowed(false);
					applianceTable.setFillsViewportHeight(true);
					applianceTable.setBorder(new EmptyBorder(0, 0, 0, 0));
					applianceTable.setBackground(UIManager.getColor("Panel.background"));
					applianceTable.setModel(new DefaultTableModel(myAgent.getApplianceArray(),new String[]{"Appliances"}) {
						private static final long serialVersionUID = 5079746104881300480L;
						@SuppressWarnings({ "rawtypes" })
						Class[] columnTypes = new Class[] {
							Appliance.class
						};
						@SuppressWarnings({ "unchecked", "rawtypes" })
						public Class getColumnClass(int columnIndex) {
							return columnTypes[columnIndex];
						}
					});
					applianceTable.getColumn("Appliances").setCellRenderer(new CheckBoxRenderer(applianceTable.getDefaultRenderer(Boolean.class)));
					applianceTable.getColumn("Appliances").setCellEditor(new ApplianceCellEditor(new JCheckBox()));
					applianceTable.setMinimumSize(new Dimension(200, applianceTable.getColumn("Appliances").getCellEditor().getTableCellEditorComponent(applianceTable, null, false, 0, 0).getMinimumSize().height*6));
					applianceTable.setPreferredScrollableViewportSize(applianceTable.getMinimumSize());
					applianceTable.setName("applianceTable");
					applianceScrollPane.setViewportView(applianceTable);
				}
			}
		}
		this.pack();
		this.setMinimumSize(this.getSize());
		this.setVisible(true);
	}
	public JLabel getLblWattageValue() {
		return lblWattageValue;
	}
	public JLabel getLblEnergyValue() {
		return lblEnergyValue;
	}
}
